package com.chenxingnet.controller;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chenxingnet.dto.Accident;
import com.chenxingnet.dto.Department;
import com.chenxingnet.dto.Drivers;
import com.chenxingnet.dto.Waybills;
import com.chenxingnet.dto.vo.AccountingControlVo;
import com.chenxingnet.dto.vo.WaybillVo;
import com.chenxingnet.service.AccidentService;
import com.chenxingnet.service.AccountingService;
import com.chenxingnet.service.DepartmentService;
import com.chenxingnet.service.DriversService;
import com.chenxingnet.service.WaybillsService;


@Controller
@RequestMapping("/accident")
public class AccidentController {
	
	@Resource
	private AccidentService accidentService;
	@Resource
	private WaybillsService waybillsService;
	@Resource
	private AccountingService accountingService;
	@Resource
	private DepartmentService departmentService;
	@Resource
	private DriversService driversService;
	
	/**
	 * 转至新增事故页面
	 * @return
	 */
	@RequestMapping("/insertAccident")
	public String insertAccident(Accident accident,HttpServletRequest request,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
			String wayid = request.getParameter("wayidSingle");
			if(!wayid.equals("")){
				int  sum   = accidentService.updateSetFasheng(Integer.parseInt(wayid));
				model.addAttribute("wayid", wayid);
				Waybills way = waybillsService.selectById(Integer.parseInt(wayid));
				model.addAttribute("way", way);
			}
		return "accident_edit";
	}
	/**
	 * 新增事故信息
	 * @return
	 */
	@RequestMapping("/addAccident")
	public String addAccident(Accident accident,HttpServletRequest request,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH");//小写的mm表示的是分钟HH:mm:ss
		
		String timeStr = request.getParameter("actime1");
		String wayid = request.getParameter("wayid");
		accident.setAcwaybill(Integer.parseInt(wayid));
		Date actime = null;
		try {
			if(!timeStr.equals("")){
				actime = sdf.parse(timeStr);
				accident.setActime(actime);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		accident.setAreportstart("1");
		
		accidentService.addAccident(accident);
		
		return "redirect:/accident/showAccident?pageNow=1";
	}
	/**
	 * 分页查询所有事故信息
	 * @return
	 */
	@RequestMapping("/showAccident")
	public String showAccident(HttpServletRequest request,Model model){
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
		} catch (Exception e) {
			
		}
		accidentService.selectAllByCount(request, model);
		return "add_accident";
	}
	
	
	

	/**
	* @Title: /waybills/mobile_showWaybill 
	*移动端事故请求
	 */
	@RequestMapping("/mobile_showAccident")
	public void mobile_showAccident(HttpServletRequest request,Model model,HttpServletResponse response){
		try {
				response.setHeader("Access-Control-Allow-Origin", "*");
				response.setContentType("application/json;charset=utf-8");
				JSONObject returnJson = new JSONObject();
				returnJson.put("falg","true");
			 
				try {
					Integer deptid = Integer.valueOf(request.getParameter("deptid"));
					if(deptid != null){
						returnJson.put("deptid", deptid);
					}
				} catch (Exception e) {			
					
				}
				
				accidentService.mobSelectAllByCount(request);
				
				returnJson.put("page", request.getAttribute("mobilePage"));
				returnJson.put("ways", request.getAttribute("ways"));
				returnJson.put("accidentList",  request.getAttribute("accidentList"));
				
				response.getWriter().print(returnJson.toString());
				
		} catch (Exception e) {  
			e.printStackTrace();  
		}
		
	}
	
	
	
	/**
	 * 在事故查询页面修改送达状态
	 * @return
	 */
	@RequestMapping("/updateSong")
	public String updateSong(HttpServletRequest request,Model model){
		String wayidSingle = request.getParameter("wayid");
		int   sum=   	waybillsService.updateByShiSongda(wayidSingle);
		try {
			Integer deptid = Integer.valueOf(request.getParameter("deptid"));
			String rolename = new String(request.getParameter("rolename").getBytes("ISO-8859-1"),"UTF-8");
			if(deptid != null){
				model.addAttribute("deptid", deptid);
			}
			if(deptid == 2 && (!rolename.equals("管理员"))){
				model.addAttribute("deptid", deptid);
				/*model.addAttribute("result", "运单已送达，请联系会计进行结算");*/
				/*return "aaa_city";*/
				return "redirect:/accident/showAccident?pageNow=1&deptid="+deptid;
			}
			if(deptid == 1 && (!rolename.equals("管理员"))){
				model.addAttribute("deptid", deptid);
				/*model.addAttribute("result", "运单已送达，请联系会计进行结算");*/
				/*return "city";*/
				return "redirect:/accident/showAccident?pageNow=1&deptid="+deptid;
			}
		} catch (Exception e) {
			
		}
		List<String>   drivers=new  ArrayList<String>();//组织返回驾驶员列表信息
	
		List<Department> deps=  departmentService.showDept();
		List<WaybillVo> wayList =    waybillsService.selectAll();
		List<Drivers>    drs=   driversService.selectAll();//获取司机信息
		List<WaybillVo> wayListRes=new  ArrayList<WaybillVo>();
		for(int  i=0;i<wayList.size();i++){
			drivers.add(wayList.get(i).getTransport());
			boolean    bo=true;   
			for(int  a=0;a<wayListRes.size();a++){
				if(wayListRes.get(a).getVehicle().equals(wayList.get(i).getVehicle())){bo=false;}
			}
			if(bo){wayListRes.add(wayList.get(i));
			}
		}
	         Set set = new  HashSet(); //驾驶员去重复
	         List newList = new  ArrayList(); 
	         for (String cd:drivers) {
	            if(set.add(cd)){
	                newList.add(cd);
	            }
	        }
		
		model.addAttribute("deps", deps);
		model.addAttribute("newList", newList);
		model.addAttribute("wayListRes", wayListRes);
		
		String deptname = request.getParameter("deptname");
		String chexing = request.getParameter("chexing");
		String xingming = request.getParameter("xingming");
		String jiesuan = request.getParameter("jiesuan");
		
		List<AccountingControlVo> accoutlist = accountingService.selectAllSingle(Integer.parseInt(wayidSingle));
		try {
			if(!deptname.equals("")){
				//accoutlist = accountingService.selectAllBySerach(deptname,chexing,xingming,jiesuan);
			}
		} catch (Exception e) {}
		
		model.addAttribute("accoutlist", accoutlist);
		AccountingControlVo   heji2=new  AccountingControlVo();
		heji2.setDeptname("合计");
		/*heji2.setBaoxian(new  BigDecimal(0));*/
		heji2.setShangxian(new BigDecimal(0));
		heji2.setQiangxian(new BigDecimal(0));
		heji2.setChangjia(new  BigDecimal(0));
		heji2.setJiashiyuan(new  BigDecimal(0));
		heji2.setLinpai(new BigDecimal(0));
		for(int y=0;y<accoutlist.size();y++){
			/*heji2.setBaoxian(heji2.getBaoxian().add(accoutlist.get(y).getBaoxian()));*/
			heji2.setShangxian(heji2.getShangxian().add(accoutlist.get(y).getShangxian()));
			heji2.setQiangxian(heji2.getQiangxian().add(accoutlist.get(y).getQiangxian()));
			heji2.setChangjia(heji2.getChangjia().add(accoutlist.get(y).getChangjia()));
			heji2.setJiashiyuan(heji2.getJiashiyuan().add(accoutlist.get(y).getJiashiyuan()));
			heji2.setLinpai(heji2.getLinpai().add(accoutlist.get(y).getLinpai()));
		}
		accoutlist.add(heji2);
		return "account_list";
	}
	
}
